import 'package:flutter/material.dart';
import 'package:test0423/screens/post_list_page.dart';
import 'video_list_page.dart';
import 'chat_list_page.dart';
import 'profile_page.dart';

class MainTabs extends StatefulWidget {
  const MainTabs({Key? key}) : super(key: key);

  @override
  _MainTabsState createState() => _MainTabsState();
}

class _MainTabsState extends State<MainTabs> with TickerProviderStateMixin {
  int _selectedIndex = 0;

  late TabController _tabController;
  late TabController _innerTabController;

  // 将 _tabs 改为方法，确保在 _innerTabController 初始化后再调用
  List<Widget> get _tabs {
    return [
      // Tab 1: Discovery without sub-tabs
      const PostListPage(),
      // Tab 2: Video Playback
      const VideoListPage(),
      // Tab 3: Chat List
      const ChatListPage(),
      // Tab 4: Personal Center
      const ProfilePage(),
    ];
  }

  @override
  void initState() {
    super.initState();
    _tabController = TabController(
      length: 4,
      vsync: this,
    ); // 初始化主 TabController
    _innerTabController = TabController(
      length: 2,
      vsync: this,
    ); // 初始化内部 TabController
  }

  @override
  void dispose() {
    _tabController.dispose();
    _innerTabController.dispose(); // 释放 _innerTabController
    super.dispose();
  }

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
      _tabController.index = index; // 更新 TabController 的 index
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: IndexedStack(
        index: _selectedIndex,
        children: _tabs,
      ),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _selectedIndex,
        onTap: _onItemTapped,
        type: BottomNavigationBarType.fixed,
        selectedItemColor: const Color(0xFFFF5A33), // 使用主色
        unselectedItemColor: Colors.grey,
        items: const [
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            label: '发现',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.play_circle_outline),
            label: '视频',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.chat),
            label: '聊天',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.person),
            label: '我的',
          ),
        ],
      ),
    );
  }
}
